/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hazardsource.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.hazardsource.domain.HiddenDanger;
import com.koron.hazardsource.service.api.HiddenDangerService;
import com.koron.hazardsource.util.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "HiddenDanger",description = "标准隐患接口类")
@RestController
@RequestMapping("/HiddenDanger")
public class HiddenDangerController {
	@Autowired
	HiddenDangerService service;

	@ApiOperation("保存草稿")
	@RequestMapping(value = "/saveDraft.htm", method = { RequestMethod.POST })
	@Log(title = "标准检查表保存草稿", businessModule = "标准隐患", businessManipulation = BusinessManipulation.INSERT)
	public MessageBean<?> saveCheckListDraft(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
			@RequestBody List<HiddenDanger> beanList){
		if(user==null){
			MessageBean<?>msg=new MessageBean<>();
			msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
			msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
			return msg;
		}
		return service.saveDraft(beanList, user);
	}

	@ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "隐患名称",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "dangerTypeId", value = "隐患类型",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "dangerLevelId", value = "隐患等级",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "firstElementId", value = "一级要素类型",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "secondElementId", value = "二级要素类型",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "description", value = "隐患描述",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "state", value = "状态",  paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "query", dataType = "Integer")})
	@ApiOperation("标准隐患表列表")
	@RequestMapping(value = "/list.htm", method = { RequestMethod.GET })
	public MessageBean<?> list(String name,String dangerTypeId,String dangerLevelId,String firstElementId,
			String secondElementId,String description,Integer state,Integer page,Integer pageSize){
		return service.list(name, dangerTypeId, dangerLevelId, firstElementId, secondElementId, description, state, page, pageSize);

	}

	@ApiOperation("提交操作")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "title", value = "标题",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "note", value = "说明",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/submit.htm", method = { RequestMethod.POST })
    @Log(title = "新增、修改隐患提交操作", businessModule = "标准隐患", businessManipulation = BusinessManipulation.INSERT)
	public MessageBean<?> submit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
			@RequestBody List<HiddenDanger> beanList,String title,String note){
		if(user==null){
			MessageBean<?>msg=new MessageBean<>();
			msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
			msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
			return msg;
		}
		return service.submit(beanList, user, title, note);
	}

	@ApiOperation("隐患废弃")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "note", value = "说明",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/discard.htm", method = { RequestMethod.POST })
    @Log(title = "隐患废弃", businessModule = "标准隐患", businessManipulation = BusinessManipulation.UPDATE)
	public MessageBean<?> discard(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@RequestParam(value = "idList")List<String> idList,String title, String note){
		if(user==null){
			MessageBean<?>msg=new MessageBean<>();
			msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
			msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
			return msg;
		}
		return service.discard(idList, user,title,note);
	}

	@ApiOperation("获取隐患详情")
	@ApiImplicitParam(name = "id", value = "隐患id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/details.htm", method = { RequestMethod.GET })
	public MessageBean<?> details(String id){
		MessageBean<HiddenDanger> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, HiddenDanger.class);
		HiddenDanger bean = service.details("hidden_danger", id);
		msg.setData(bean);
        return msg;
	}

	@ApiOperation("隐患导入")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "title", value = "标题",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "note", value = "说明",  paramType = "query", dataType = "String")})
    @RequestMapping(value = "/improtExcel.htm", method = { RequestMethod.POST })
    @Log(title = "隐患导入", businessModule = "标准隐患", businessManipulation = BusinessManipulation.INSERT)
	public MessageBean<?> improtExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
			MultipartFile file,String title,String note){
		if(user==null){
			MessageBean<?>msg=new MessageBean<>();
			msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
			msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
			return msg;
		}
		return service.importExcel(user, title, note, file);
	}

	@ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "隐患名称",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "dangerTypeId", value = "隐患类型",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "dangerLevelId", value = "隐患等级",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "firstElementId", value = "一级要素类型",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "secondElementId", value = "二级要素类型",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "description", value = "隐患描述",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "state", value = "状态",  paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "query", dataType = "Integer")})
	@ApiOperation("隐患列表导出")
	@RequestMapping(value = "/exports.htm", method = { RequestMethod.GET })
	public HttpEntity<?> exports(String name,String dangerTypeId,String dangerLevelId,String firstElementId,
			String secondElementId,String description,Integer state,Integer page,Integer pageSize){
		List<HiddenDanger> list = service.queryList(name, dangerTypeId, dangerLevelId, firstElementId, secondElementId, description, state, page, pageSize);
		Map<String, Object> map = new HashMap<>();
		map.put("data",list);
        File targetFile = new File("static/excel/stdDanger.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/stdDanger.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            return null;
        }
		return Excel.createFile("隐患列表",targetFile, "stdDanger.btl",map);
	}

	@ApiOperation("标准隐患草稿列表")
    @RequestMapping(value = "/getDraftList.htm", method = { RequestMethod.GET })
    public MessageBean<?> getDraftList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user){
		if(user==null){
			MessageBean<?>msg=new MessageBean<>();
			msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
			msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
			return msg;
		}
		return service.getDraftList(user);
    }

	@ApiOperation("流程中的业务数据")
    @ApiImplicitParam(name = "ids", value = "流程关联的业务ids", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getProcessData.htm", method = {RequestMethod.GET})
    public MessageBean<?> getProcessData(String ids){
    	return service.getProcessData(ids);
    }

	@ApiOperation("历史版本")
    @ApiImplicitParam(name = "code", value = "隐患code", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getHistoryList.htm", method = {RequestMethod.GET})
    public MessageBean<?> getHistoryList(String code){
    	return service.getHistoryList(code);
    }

	@ApiOperation(value = "下载隐患excel模板")
    @RequestMapping(value = "/downloadtemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> downloadTemplate() {
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/stdDanger.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/stdDanger.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            return null;
        }
        return Excel.createFile("隐患模板", targetFile, "default.btl", map);
    }


	@ApiOperation("删除草稿")
	@ApiImplicitParam(name = "id", value = "id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/deleteDraft.htm", method = { RequestMethod.GET })
	public MessageBean<?> deleteDraft(String id){
		return service.deleteDraft(id);
	}

	@ApiOperation("新增/修改隐患台账时填写描述时调用该接口")
	@ApiImplicitParam(name = "description", value = "标准隐患的描述",  paramType = "query", dataType = "String")
	@RequestMapping(value = "/queryByDescription.htm", method = { RequestMethod.GET })
	public MessageBean<List<HiddenDanger>> queryByDescription(@RequestParam(required = false) final String description){
		return  service.queryByDescription(description);
	}
}
